package com.only4play.user.config;

import com.only4play.common.PasswordGenerator;
import com.only4play.idgenerator.provider.IdGenerator;
import com.only4play.idgenerator.provider.IdGeneratorOptions;
import com.only4play.idgenerator.provider.IdGeneratorProvider;
import com.only4play.idgenerator.strategy.IdGenerateStrategy;
import com.only4play.jpa.support.interceptor.AggregateAutofillInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author liyuncong
 * @version 1.0
 * @file UserBundleConfiguration.java
 * @brief UserBundleConfiguration
 * @details UserBundleConfiguration
 * @date 2022-04-01
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-04-01                  liyuncong          Created
 */
@Configuration
@EnableJpaRepositories(basePackages = {"com.only4play.user.repository"})
@ComponentScan(basePackages = {"com.only4play.user"})
public class UserServerConfiguration {

    /**
     * 指定加密方式.
     */
    @Bean("passwordEncoder")
    public PasswordEncoder passwordEncoder() {
        // 使用BCrypt加密密码
        return new BCryptPasswordEncoder();
    }

    @Bean("passwordGenerator")
    public PasswordGenerator passwordGenerator() {
        // 使用自定义密码生成器
        return new PasswordGenerator() {
        };
    }

    /**
     * 唯一ID生成器实现类
     *
     * @return IdGeneratorProvider
     */
    @Bean
    public IdGeneratorProvider idGeneratorProvider() {
        IdGeneratorOptions options = new IdGeneratorOptions();
        // 设置生成策略为UUID
        options.setGenerateStrategy(IdGenerateStrategy.UUID);
        return new IdGeneratorProvider(options);
    }

    /**
     * 唯一ID生成器
     *
     * @return IdGenerator
     */
    @Bean("idGenerator")
    public IdGenerator idGenerator() {
        return idGeneratorProvider().instance();
    }

    @Bean
    public AggregateAutofillInterceptor autofillInterceptor() {
        return new AggregateAutofillInterceptor();
    }
}
